export default {
  state: {
    name: "李狗娜",
    info: {},
  },
  getters: {
    getUserRouters(state) {
      console.log(state)
      return state.info.routers;
    },
  },
  mutations: {
    // 模拟后端接口返回用户信息及权限路由，存入localStorage和store的state中
    getUesrInfo(state) {
      let res = {
        data: {
          userInfo: {
            name: "张三",
            age: "21",
            token:
              "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbIm9wZW5pZCJdLCJleHAiOjE3NDA3MTk2OTcsImlhdCI6MTc0MDExNDg5NywiYXV0aG9yaXRpZXMiOlsiUFJPQ0VTU19BRE1JTiIsIlJPTEVfQURNSU4iLCJERVZPUFNfQURNSU4iXSwianRpIjoiNzI5MzYyYzAtZjhiNC00MTk4LWIyZTItOGJmNWE2YTAzNGQ5IiwiY2xpZW50X2lkIjoid2ViX2FwcCJ9.OKf2fFPWG76Qgx__-kkJ3b8e9rRGIoTZ2sRIh70I4Ml9JNVokgF8NqU9Ez9epBSbK31D-6Ee8MFiuGl3k4ofb9ZpQP-aVsvOfsW-_iTthWOIoqevQntgB_BZ_sNBfkp3xcqkGrC2X4ie4_Vtz8tdP-aEWg0xY2o_Ck9Ij0NFKqdD9lSpztSEeuAFODkY_cMnViADUq0AjUJNZJfQRMhqAQtqeTJ0R3HZD3gd1kntcRp_2T-B9zTRFTBw-praHszeA-5PU1yt41FKE3nqb7hiDdEq4WbGA0kQwi0WV74GbQtY-lBWfpO_JcQ0_FZDYNMJLohqYVuzMlmS_TcdXsdUBQ",
          },
          routers: ["/index", "/about"],
        },
      };
      let promise = new Promise((resolve, reject) => {
        resolve(res);
      });
      promise
        .then((res) => {
          localStorage.setItem("userInfo", JSON.stringify(res.data));
          state.info = res.data;
        })
        .catch((err) => console.log(err));
    },
    // 获取token存入localStorage和store的state中
    setUserToken(state) {
      let token =
        "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX25hbWUiOiJhZG1pbiIsInNjb3BlIjpbIm9wZW5pZCJdLCJleHAiOjE3NDA3MTk2OTcsImlhdCI6MTc0MDExNDg5NywiYXV0aG9yaXRpZXMiOlsiUFJPQ0VTU19BRE1JTiIsIlJPTEVfQURNSU4iLCJERVZPUFNfQURNSU4iXSwianRpIjoiNzI5MzYyYzAtZjhiNC00MTk4LWIyZTItOGJmNWE2YTAzNGQ5IiwiY2xpZW50X2lkIjoid2ViX2FwcCJ9.OKf2fFPWG76Qgx__-kkJ3b8e9rRGIoTZ2sRIh70I4Ml9JNVokgF8NqU9Ez9epBSbK31D-6Ee8MFiuGl3k4ofb9ZpQP-aVsvOfsW-_iTthWOIoqevQntgB_BZ_sNBfkp3xcqkGrC2X4ie4_Vtz8tdP-aEWg0xY2o_Ck9Ij0NFKqdD9lSpztSEeuAFODkY_cMnViADUq0AjUJNZJfQRMhqAQtqeTJ0R3HZD3gd1kntcRp_2T-B9zTRFTBw-praHszeA-5PU1yt41FKE3nqb7hiDdEq4WbGA0kQwi0WV74GbQtY-lBWfpO_JcQ0_FZDYNMJLohqYVuzMlmS_TcdXsdUBQ";
      let info = JSON.parse(localStorage.getItem("userInfo"));
      info.userInfo.token = token;
      localStorage.setItem("userInfo", JSON.stringify(info));
      state.info = info;
    },
  },
  actions: {
    GetUesrInfo(dispatch) {
      dispatch.commit("getUesrInfo");
    },
    SetUserToken(dispatch) {
      if (localStorage.getItem("userInfo")) {
        dispatch.commit("setUserToken");
      } else {
        dispatch.commit("getUesrInfo");
      }
    },
  },
};
